import Vue from 'vue';
import Vuex from 'vuex';
import hashCode from '@/libs/hashCode';

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        recordList: [] as RecordItem[]
    } as RootState,
    mutations: {
        fetchRecords(state) {
            state.recordList = JSON.parse(window.localStorage.getItem('recordList')!) || [];
        },
        createRecord(state, record) {
            record.hashCode = hashCode(JSON.stringify(record));
            state.recordList = [...state.recordList, record];
            store.commit('saveRecords');
        },
        saveRecords(state) {
            window.localStorage.setItem('recordList', JSON.stringify(state.recordList));
        },
        clearRecords(state) {
            window.localStorage.clear();
        }
    },
    actions: {},
    modules: {}
});

export default store
